package com.example.generator.utils;

import com.example.generator.entity.FieldInfo;
import org.thymeleaf.util.ArrayUtils;

import java.util.List;

/**
 * @author：QPX
 * @date：2024-01-26-15:28
 */
public class FieldTypeUtil {
    public static final String[] SQL_STRING_TYPE = new String[]{"char", "varchar", "text", "mediumtext", "longtext"};
    public static final String[] SQL_DATE_TYPE = new String[]{"date"};
    public static final String[] SQL_DATE_TIME_TYPE = new String[]{"datetime", "timestamp"};
    public static final String[] SQL_INTEGER_TYPE = new String[]{"int", "tinyint"};
    public static final String[] SQL_LONG_TYPE = new String[]{"bigint"};

    public static List<FieldInfo> typeMapping(List<FieldInfo> fieldInfoList) {
        fieldInfoList.forEach(fieldInfo -> {
            String type = fieldInfo.getType().split("\\(")[0];
            if (ArrayUtils.contains(SQL_DATE_TYPE, type) || ArrayUtils.contains(SQL_DATE_TIME_TYPE, type)) {
                fieldInfo.setIsContainDate(true);
                fieldInfo.setJavaType("Date");
            } else if (ArrayUtils.contains(SQL_STRING_TYPE, type)) {
                fieldInfo.setJavaType("String");
            } else if (ArrayUtils.contains(SQL_INTEGER_TYPE, type)) {
                fieldInfo.setJavaType("Integer");
            } else if (ArrayUtils.contains(SQL_LONG_TYPE, type)) {
                fieldInfo.setJavaType("Long");
            }else{
                fieldInfo.setJavaType("String");
            }
        });
        return fieldInfoList;
    }
}
